//
//  ViewController.m
//  FeedBackDemo
//
//  Created by sanfang on 16/11/24.
//  Copyright © 2016年 ziz. All rights reserved.
//

#import "ViewController.h"

#import <YWFeedbackFMWK/YWFeedbackKit.h>
#import <YWFeedbackFMWK/YWFeedbackViewController.h>
#import "TWMessageBarManager.h"


#import "FeedbackTool.h"

/**
 *  修改为你自己的appkey。
 *  同时，也需要替换yw_1222.jpg这个安全图片。
 */
static NSString * const kAppKey = @"23015524";

@interface ViewController ()

@property (nonatomic, strong) YWFeedbackKit *feedbackKit;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)feedBackAction:(id)sendere
{
//    YWLoginController * login = [[YWLoginController alloc]initWithNibName:@"YWLoginController" bundle:nil ];
//    self.navigationController.navigationBar.hidden = YES;
//    [self.navigationController pushViewController:login animated:YES];
    
    
//    [self openFeedbackViewController];
    __weak typeof(self) weakSelf = self;
    [[FeedbackTool sharedTool]openFeedbackViewController:^(BCFeedbackViewController *controller) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
        [weakSelf presentViewController:nav animated:YES completion:nil];
        
        [controller setCloseBlock:^(UIViewController *aParentController){
            [aParentController dismissViewControllerAnimated:YES completion:nil];
        }];
    }];
}
#pragma mark - methods
/** 打开用户反馈页面 */
- (void)openFeedbackViewController
    {
    //  初始化方式,或者参考下方的`- (YWFeedbackKit *)feedbackKit`方法。
//    self.feedbackKit = [[YWFeedbackKit alloc] initWithAppKey:kAppKey];
    
    /** 设置App自定义扩展反馈数据 */
    self.feedbackKit.extInfo = @{@"loginTime":[[NSDate date] description],
                                 @"visitPath":@"登陆->关于->反馈",
                                 @"userid":@"yourid",
                                 @"应用自定义扩展信息":@"开发者可以根据需要设置不同的自定义信息，方便在反馈系统中查看"};
    
    __weak typeof(self) weakSelf = self;
    [self.feedbackKit makeFeedbackViewControllerWithCompletionBlock:^(YWFeedbackViewController *viewController, NSError *error) {
        if (viewController != nil) {
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
            [weakSelf presentViewController:nav animated:YES completion:nil];
            
            [viewController setCloseBlock:^(UIViewController *aParentController){
                [aParentController dismissViewControllerAnimated:YES completion:nil];
            }];
        } else {
            /** 使用自定义的方式抛出error时，此部分可以注释掉 */
//            NSString *title = [error.userInfo objectForKey:@"msg"]?:@"接口调用失败，请保持网络通畅！";
//            [[TWMessageBarManager sharedInstance] showMessageWithTitle:title
//                                                           description:nil
//                                                                  type:TWMessageBarMessageTypeError];
        }
    }];
    
    /** 使用自定义的方式抛出error */
       [self.feedbackKit setYWFeedbackViewControllerErrorBlock:^(YWFeedbackViewController *viewController, NSError *error) {
//            NSString *title = [error.userInfo objectForKey:@"msg"]?:@"接口调用失败，请保持网络通畅！";
//            [[TWMessageBarManager sharedInstance] showMessageWithTitle:title
//                                                           description:[NSString stringWithFormat:@"%ld", error.code]
//                                                                  type:TWMessageBarMessageTypeError];
        }];
}


#pragma mark getter
- (YWFeedbackKit *)feedbackKit {
    if (!_feedbackKit) {
        _feedbackKit = [[YWFeedbackKit alloc] initWithAppKey:kAppKey];

    }
    return _feedbackKit;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
